package com.ny.controller;

import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private UserService userService;

    @RequestMapping("adminLogin")
    public String adminLogin(@RequestParam String username, @RequestParam String password ,Model model) {
        Users users  = userService.login(username,password);
        if (users!=null&& users.getRole().equals("管理员")){
            List<Reservations> reservationList = reservationService.findAllReservations();
            model.addAttribute("reservationList", reservationList);
            return "reservations";
        }
       return "login";
    }


    @RequestMapping("/approveReservation")
    public String approveReservation(@RequestParam("reservationId")int reservationId,int classroomId ){

        reservationService.updateReservationStatus(reservationId ,"已通过","null");
        classroomService.updateClassroomStatusById(classroomId,"已预约");
        return "redirect:/reservationList";
    }

    @RequestMapping ("/rejectReservation")
    public String rejectReservation(@RequestParam("reservationId")int reservationId,@RequestParam("classroomId")int classroomId,@RequestParam("rejectReason") String rejectReason){
        System.out.println(reservationId);
        System.out.println(classroomId);
        System.out.println(rejectReason);
        reservationService.updateReservationStatus(reservationId,"驳回",rejectReason);
        classroomService.updateClassroomStatusById(classroomId,"不通过");
        return "redirect:/reservations";
    }
    //教室管理
    @RequestMapping("/classroomManagement")
    public String classroomManagement(){
        return "classroomManagement";
    }
}
